<template>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <!-- prop: -->
      <el-table-column prop="id" label="ID" width="180"> </el-table-column>
      <el-table-column prop="specsname" label="属性名称" width="200">
      </el-table-column>
      <el-table-column 
      prop="attrs" 
      label="属性值" 
      width="200">
      <template slot-scope="scope">
        <el-tag 
        type="success" 
        v-for="(item,index) in scope.row.attrs" 
        :key="index"
        >{{item}}</el-tag>
      </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="180">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status === 1">正常</el-tag>
          <el-tag type="danger" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="edit(scope.row)"
            circle
          ></el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="del(scope.row.id)"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- size-change:切换每一页显示多少条 -->
    <!-- current-change：切换页码 -->
    <!-- current-page.sync：当前页码-->
    <!-- page-size：每页显示几条 -->
    <!-- total：总条数 -->
    <!-- <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="currentPage"
      :page-size="size"
      layout="prev, pager, next, jumper,total"
      :total="total"
    >
    </el-pagination> -->
  </div>
</template>

<script>
import { getCount, getSpecsList, specsDel } from "../../request";
export default {
  data() {
    return {
      tableData: [],
      //   isShow: true,
      //   isDisabled: true,
      //默认显示第一页
      // currentPage: 1,
      //每页条数
      // size: 2,
      //总条数
      // total: 1,
    };
  },
  created() {
    //获取总条数
    this.$bus.$on("update", () => {
      this.getTotalNum();
    });
    //获取总条数
    this.getTotalNum();
  },
  methods: {
    //需要先获取总数量，才能计算分成多少页，然后查看某一页
    async getTotalNum() {
      let res = await getCount();
      this.total = res.list[0].total;
      //获取总数量之后，调用分页得请求
      this.getUserList();
    },
    async getUserList() {
      let res = await getSpecsList();
      this.tableData = res.list;
    },
    edit(info) {
      this.$emit("edit", { ...info });
    },
    //删除菜单弹窗
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })

        //点击确定
        .then(() => {
          this.specsdel(id);
        })
        //点击取消
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //真的要删了
    async specsdel(id) {
      let res = await specsDel(id);
      this.tableData = res.list;
      this.getTotalNum();
    },
    // 切换页码时调用
    handleCurrentChange(val) {
      this.getUserList();
    },
    check(id) {
      this.dialogVisible = true;
    },
  },
};
</script>

<style>
</style>